package cn.com.bsfit.netty.resp;

import cn.com.bsfit.netty.config.constants.ErrorCodeEnums;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 类名 BaseResponse
 * 描述 基本返回类(默认)
 *
 * @author hedonglin
 * @version 1.0
 * @date 2019/9/29 11:57
 */
public class BaseResponse extends HashMap<String, Object> implements Serializable {
    private static final long serialVersionUID = 196181903563317244L;
    public static final Integer SUCCESS_CODE = ErrorCodeEnums.SUCCESS.getErrCode();
    public static final Integer REQ_PARAM_ERR_CODE = ErrorCodeEnums.REQ_PARAM_ERROR.getErrCode();
    public static final Integer SERVER_INNER_ERR_CODE = ErrorCodeEnums.SERVER_INNER_ERROR.getErrCode();

    /** 返回是否成功的标志 */
    public static final String SUCCESS_KEY = "success";
    /** 返回状态码的键 */
    public static final String ERR_CODE_KEY = "errCode";
    /** 返回的错误消息 */
    public static final String ERR_MSG_KEY = "errMsg";
    /** 返回的数据 */
    public static final String DATA_KEY = "data";

    public BaseResponse success(){
        BaseResponse response = new BaseResponse();
        response.put(SUCCESS_KEY, true);
        response.put(ERR_CODE_KEY, SUCCESS_CODE);
        response.put(ERR_MSG_KEY, "success");
        response.put(DATA_KEY, Collections.emptyMap());
        return response;
    }

    public BaseResponse success(int errCode){
        BaseResponse response = success();
        response.put(ERR_CODE_KEY, errCode);
        return response;
    }

    public BaseResponse success(int errCode, String errMsg){
        BaseResponse response = success(errCode);
        response.put(ERR_MSG_KEY, errMsg);
        return response;
    }

    public BaseResponse success(Map<String, Object> data){
        BaseResponse response = success();
        response.put(DATA_KEY, data);
        return response;
    }

    public BaseResponse success(int errCode, Map<String, Object> data){
        BaseResponse response = success(data);
        response.put(ERR_CODE_KEY, errCode);
        return response;
    }

    public BaseResponse error(int errCode){
        BaseResponse response = new BaseResponse();
        response.put(SUCCESS_KEY, false);
        response.put(ERR_CODE_KEY, errCode);
        response.put(ERR_MSG_KEY, "fail");
        return response;
    }

    public BaseResponse error(int errCode, String errMsg){
        BaseResponse response = error(errCode);
        response.put(ERR_MSG_KEY, errMsg);
        return response;
    }

    public static BaseResponse builder(){
        return new BaseResponse();
    }
}
